<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/code.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%
	String source = input.getDesc("code", "");
	Message codeMsg = decodeQrCode(source);
	// 二维码信息有效
	if (codeMsg.getErrcode() == 0) {
		Map codeMap = (Map)codeMsg.get("code");
		String id = Tools.getDesc(codeMap.get("id"));
		String type = Tools.getDesc(codeMap.get("type"));
		// 科室二维码
		if (type.equals("dept")) {
			List<Hospital> list = QBuilder.db("basedb").select().from("Hospital").where(new Where("id=", id).and("status=", STATUS_ENABLE)).query().getResultObj(); 
			if (isEmpty(list)) {
				msg.setErrcodeAndErrmsg(-1, "没有找到你要关注的科室");
				Tools.encryptPrint(out, msg);
				return;
			}
			Hospital hh = list.get(0);
			Map dataMap = new HashMap();
			dataMap.put("id", hh.getId());
			dataMap.put("category", DEPT_CATEGORY);
			dataMap.put("dept_name", hh.getName());
			dataMap.put("hospital_name", hh.getDescr());
			dataMap.put("avatar", Tools.getDesc(hh.getJson("avatar"), "http://cdn-img.easyicon.net/png/11907/1190716.gif"));
			dataMap.put("tips", String.format("您确定要关注%s的%s吗？", new Object[]{hh.getDescr(),  hh.getName()}));
			msg.put("data", dataMap);
		}
		// 医生二维码
		if (type.equals("doctor")) {
			List<Map> list = QBuilder.db("basedb").select("new map(u.id as id,u.name as name, u.avatar as avatar, u.title as title, h.name as dept_name, h.descr as hospital_name)").from("Hospital as h, User as u").where(new Where("u.id=", id).and("u.deptId=h.hospital").and("u.status=", STATUS_ENABLE)).query().getResultObj(); 
			if (isEmpty(list)) {
				msg.setErrcodeAndErrmsg(-1, "没有找到你要关注的医生");
				Tools.encryptPrint(out, msg);
				return;
			}
			Map dataMap = list.get(0);
			dataMap.put("category", SECTION_DOCTOR);
			dataMap.put("tips", String.format("您确定要关注%s医生及其所属的%s%s吗？", new Object[]{Tools.getDesc(dataMap.get("name")), Tools.getDesc(dataMap.get("hospital_name")), Tools.getDesc(dataMap.get("dept_name"))}));
			msg.put("data", dataMap);
		}
		// 用户二维码
		if (type.equals("patient")) {
			List<Map> list = QBuilder.db("basedb").select("new map(u.id as id, case when u.nickName is null then u.userName else u.nickName end  as user_name, u.name as name, u.avatar as avatar, u.title as title)").from("Hospital as h, User as u").where(new Where("u.id=", id).and("u.hospital=h.hospital").and("u.status=", STATUS_ENABLE)).query().getResultObj(); 
			if (isEmpty(list)) {
				msg.setErrcodeAndErrmsg(-1, "没有找到你要关注的用户");
				Tools.encryptPrint(out, msg);
				return;
			}
			Map dataMap = list.get(0);
			dataMap.put("category", SECTION_PATIENT);
			dataMap.put("tips", String.format("您确定要关注%s吗？", new Object[]{Tools.getDesc(dataMap.get("user_name"))}));
			msg.put("data", dataMap);
		}
		Tools.encryptPrint(out, msg);
		return;
	}
	Tools.encryptPrint(out, codeMsg);
%>
